$(document).ready(function() {
$("#questionBank").click(function(){
    $("#t1 tr:not(:first)").empty();
    $.ajax({
        type : "get",
        url : "/teamExam/getQuestion",
        dataType : "json",
        data:{
            pageNo:1
        },
        success : function(data) {
            for(var i in data){
                $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                    "<td>"+data[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
            }
           $("#currentPage").html("1");
            if(1==parseInt($("#currentPage").html().toString())){
                $("#previousPage").addClass("disabled");
            }

            $(".chooseQuestion").click(function(){
                var id=$(this).parent().parent().find("td").eq(0).text();
                var content=$(this).parent().parent().find("td").eq(1).text();

                $("#tques").append("<tr><td style='display: none'>" + id+ "</td>" +
                    "<td>"+content+"</td><td >\<input type='button' class='deleteQuestion' value='移除'>\</td></tr>");
                    $(".deleteQuestion").click(function(){
                       $(this).parent().parent().remove();
                    })

            });
            console.log(data);
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });
});

    $("#homePage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").addClass("disabled");
        $("#nextPage").removeClass("disabled");
        $.ajax({
            type : "get",
            url : "/teamExam/getQuestion",
            dataType : "json",
            data:{
                pageNo:1
            },
            success : function(data) {
                for(var i in data){
                    $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                        "<td>"+data[i].content+"</td><td >\<input type='button' id='addQuestion' value='添加'>\</td></tr>");
                }
                $("#currentPage").html("1");
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#endPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").removeClass("disabled");
        $("#nextPage").addClass("disabled");
            $.ajax({
                type : "get",
                url : "/teamExam/getEndPage",
                dataType : "json",
                success : function(data) {
                    for(var i in data.sumData){
                        $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                            "<td>"+data.sumData[i].content+"</td><td >\<input type='button' id='addQuestion' value='添加'>\</td></tr>");
                    }
                    currentPage=$("#currentPage").html(data.pageNo);
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
    });
    $("#previousPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#nextPage").removeClass("disabled");
        var currentPage=$("#currentPage").html();
        var pageNo=parseInt(currentPage.toString())-1;
        $.ajax({
            type : "get",
            url : "/teamExam/getPreviousPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data){
                    $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                        "<td>"+data[i].content+"</td><td >\<input type='button' id='addQuestion' value='添加'>\</td></tr>");
                }
                $("#currentPage").html(pageNo);
                if(1==pageNo){
                    $("#previousPage").addClass("disabled");
                }else{
                    $("#previousPage").removeClass("disabled");
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#nextPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").remove("disabled");
        var currentPage=$("#currentPage").html();
        var pageNo=parseInt(currentPage.toString())+1;
        $.ajax({
            type : "get",
            url : "/teamExam/getNextPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data.sumData){
                    $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                        "<td>"+data.sumData[i].content+"</td><td >\<input type='button' id='addQuestion' value='添加'>\</td></tr>");
                }
                $("#currentPage").html(pageNo);
                if(data.pageNo==pageNo){
                    $("#nextPage").addClass("disabled");
                }else{
                    $("#previousPage").removeClass("disabled");
                    $("#nextPage").removeClass("disabled");
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#skipPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        var currentPage=$("#text").val();
        var pageNo=parseInt(currentPage.toString());
        if(isNaN(pageNo)){
            alert("请输入数字");
        }
        $.ajax({
            type : "get",
            url : "/teamExam/getNextPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data.sumData){
                    $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                        "<td>"+data.sumData[i].content+"</td><td >\<input type='button' id='addQuestion' value='添加'>\</td></tr>");
                }

                $("#currentPage").html(pageNo);
                if(data.pageNo<pageNo){
                    alert("输入页数超出范围,请重新输入!");
                }
                if(1==pageNo){
                    $("#previousPage").addClass("disabled");
                    $("#nextPage").removeClass("disabled");
                    return;
                }
                if(data.pageNo==pageNo){
                    $("#nextPage").addClass("disabled");
                    $("#previousPage").removeClass("disabled");
                    return;
                }
                else{
                    $("#previousPage").removeClass("disabled");
                    $("#nextPage").removeClass("disabled");
                    return;
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#addQuestion").click(function(){
        var content=$("#question").val();
        $.ajax({
            type : "get",
            url : "/teamExam/addQuestion",
            dataType : "text",
            data:{
                content:content
            },
            success : function(data) {
                $("#question").val("");
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });
});